<template>
  <!-- 双柱状图+折线图 -->
  <div class="cbox" :style="`width: ${width};height:${height}`">
    <div class="six" ref="six"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    legendData: {
      type: Array,
      default: () => {
        return ["本期数据", "历史数据", "偏差值"];
      },
    },

    xData: {
      type: Array,
      default: () => {
        return ["1月", "2月", "3月", "4月", "5月", "6月", "7月"];
      },
    },

    yData1: {
      type: Array,
      default: () => {
        return [400, 400, 300, 300, 300, 400, 400, 400, 300];
      },
    },

    yData2: {
      type: Array,
      default: () => {
        return [400, 500, 500, 500, 500, 400, 400, 500, 500];
      },
    },

    yData3: {
      type: Array,
      default: () => {
        return [400, 600, 700, 700, 1000, 400, 400, 600, 700];
      },
    },

    width: {
      type: String,
      default: "800px",
    },

    height: {
      type: String,
      default: "700px",
    },
  },
  data() {
    return {};
  },
  methods: {
    initChart() {
      const myChart = echarts.init(this.$refs.six);
      myChart.setOption(this.pieOption);
    },
  },
  mounted() {
    this.initChart();
  },
  updated() {
    this.initChart();
  },
  computed: {
    pieOption() {
      return {
        backgroundColor: "transparent",
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            textStyle: {
              color: "#fff",
            },
          },
        },
        grid: {
          borderWidth: 0,
          top: 75,
          left: 50,
          right: 70,
          bottom: 95,
          textStyle: {
            color: "#fff",
          },
        },
        legend: {
          data: this.legendData,
          top: 0,
          right: "1.5%",
          icon: "path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z",
          textStyle: {
            color: "#90979c",
          },
          // itemWidth: 10,
          // itemHeight: 10,
        },
        xAxis: {
          type: "category",
          data: this.xData,
          axisLine: {
            lineStyle: {
              color: "rgba(204,187,225,0.5)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(77, 128, 254, 0.2)",
              width: 2,
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: [
          {
            type: "value",
            max: "1200",
            axisLine: {
              lineStyle: {
                color: "rgba(204,187,225,0.5)",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(77, 128, 254, 0.2)",
              },
            },
            axisLabel: {
              color: "#999",
              textStyle: {
                fontSize: 12,
              },
            },
            axisTick: {
              show: true,
              alignWithLabel: true,
            },
          },
        ],
        dataZoom: [
          {
            show: true,
            height: 25,
            xAxisIndex: [0],
            bottom: 18,
            start: 0,
            end: 100,
            handleIcon:
              "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleSize: "110%",
            handleStyle: {
              color: "#d3dee5",
            },
            textStyle: {
              color: "#fff",
            },
            fillerColor: "rgba(77, 128, 254, 0.2)",
            borderColor: "#90979c",
          },
          {
            type: "inside",
            show: true,
            height: 15,
            start: 1,
            end: 35,
          },
        ],
        series: [
          {
            name: this.legendData[0],
            type: "bar",
            barWidth: 20,
            itemStyle: {
              normal: {
                color: "rgba(0,192,233,1)",
                // barBorderRadius: [12, 12, 0, 0],
              },
            },
            data: this.yData1,
          },
          {
            name: this.legendData[1],
            type: "bar",
            barWidth: 20,
            itemStyle: {
              normal: {
                color: "rgba(0,150,243,1)",
                // barBorderRadius: [12, 12, 0, 0],
              },
            },
            data: this.yData2,
          },
          {
            name: this.legendData[2],
            type: "line",
            barWidth: 20,
            smooth: true,
            symbolSize: 5, //折线上圆角大小
            symbol: "circle",
            itemStyle: {
              normal: {
                color: "rgba(26,216,175,1)",
                // barBorderRadius: [12, 12, 0, 0],
              },
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "RGBA(26,216,175,0.5)",
                },
                {
                  offset: 1,
                  color: "RGBA(26,216,175,0)",
                },
              ]),
            },
            data: this.yData3,
          },
        ],
      };
    },
  },
};
</script>

<style lang="less" scoped>
.cbox {
  // width: 600px;
  // height: 600px;
}

.six {
  width: 100%;
  height: 100%;
}
</style>
